import time

from po.certification import CertiFication
from po.couse import Course
from po.guihua_teacher import ShengyaTeacher
from po.index_page import IndexPage
from po.kaobian import Kaobian
from po.kaoyan import KaoYan
from po.shengben import Shengben
from po.zxx_base import Zxx_Base
from tools.base_page import BasePage
from tools.mysql_tools import Dbmysql



class Base_Main(BasePage):
    mm = ("xpath", "//*[@text='密码登录']")
    username = ("id", "auth_phone_input_edt")
    password = ("id", "auth_password_input_edt")
    denglu = ("xpath", "//*[@text='登录']")
    shezhi = ("xpath", "//*[@text='设置']")
    user_nickname_view = ("id", "com.znb.zxx:id/user_name_view")
    wode = ("xpath", "//*[@text='我的']")
    qlogin = ("id", "user_icon_view")
    user_exit_view = ("id", "setting_logout_btn")
    bianji = ("xpath", "//*[@text='编辑个人信息']")
    get_code = ("id", "auth_phone_confirm_view")
    input_code = ("id", "code_view")

    # register_locator
    register_locator = ('id', 'com.znb.zxx:id/new_user_registered_tv')
    input_password_locator = ('id', 'password_input_ed')
    wancheng_locator = ('id', 'auth_password_confirm_view')

    # get_back_password_locator
    forget_password = ('id', 'forget_password_tv')
    new_password = ('id', 'password_input_ed')
    next_step = ('xpath', '//*[@text="下一步"]')
    # 确认密码
    confirm_password = ('id', 'password_input_check_ed')
    submit = ('id', 'auth_password_confirm_view')

    def goto_person(self):
        from po.po_person import PerSon
        return PerSon(self.driver)

    def login_notreg(self, u, p):

        '''
        显示等待判断账号的登录状态，如果未登录账号执行登录，已登录时退出后继续登录
        '''
        self.find(self.wode).click()
        if self.find(self.user_nickname_view).text == "未登录":
            self.find(self.qlogin).click()
            self.find(self.mm).click()
            uname = self.judge_locator_visible(self.username)
            uname.send_keys(u)
            pword = self.judge_locator_visible(self.password)
            pword.send_keys(p)
            self.find(self.denglu).click()
        else:
            self.find_by_scroll('设置').click()
            self.find(self.user_exit_view).click()
            self.find_by_scroll('未登录').click()
            self.find(self.mm).click()
            uname = self.judge_locator_visible(self.username)
            time.sleep(1)
            uname.send_keys(u)
            pword = self.judge_locator_visible(self.password)
            time.sleep(1)
            pword.send_keys(p)
            self.find(self.denglu).click()
        return self.try_login_and_back()

    def try_login_and_back(self):
        if self.is_element(self.bianji) == True:
            print('登录成功')
            return True
        else:
            self.page(self.index)
            return False

    def login_succeed(self, u, p):  # 账号成功
        ac = self.driver.current_activity
        self.driver.wait_activity(ac, timeout=5)
        self.find_element(self.driver, self.username).send_keys(u)
        self.find_element(self.driver, self.password).send_keys(p)
        self.find_element(self.driver, self.denglu).click()
        self.find(self.shezhi).click()
        self.find(self.user_exit_view).click()
        self.is_element(self.bianji) == True
        return True

    def mimacuowu(self, u, p):  # 账号错误
        ac = self.driver.current_activity
        self.driver.wait_activity(ac, timeout=5)
        self.find_element(self.driver, self.username).send_keys(u)
        self.find_element(self.driver, self.password).send_keys(p)
        self.find_element(self.driver, self.denglu).click()
        return self.get_toast_text()

    def waitpay(self):
        waitpay = ("xpath", "//*[contains(@text,'待支付')]")
        kc_title = ("id", "com.znb.zxx:id/tv_pay")
        self.click_element(self.driver, waitpay)
        paytext = self.find_element(self.driver, kc_title).text
        return paytext

    def code_login(self, mobile):
        '''
        老用户使用验证登录
        :return:
        '''
        quanxian = ('id', "com.android.permissioncontroller:id/permission_allow_button")
        self.find(self.qlogin).click()
        ac = self.driver.current_activity
        self.driver.wait_activity(ac, timeout=5)
        self.find(self.username).send_keys(mobile)
        self.find(self.get_code).click()
        if self.is_element(quanxian) == True:
            self.find(quanxian).click()
        code = Dbmysql().query(
            'select content  from zxx_sms_log where mobile = {}  ORDER BY sendtime desc LIMIT 1 '.format(mobile))
        code_data = code[0][0]
        a, b = code_data.split('|', 1)
        self.find(self.input_code).send_keys(b)
        if self.is_element(self.bianji):
            self.find(self.shezhi).click()
            self.find(self.user_exit_view).click()
            return True
        else:
            return False

    def register_user(self, mobile, password):
        self.find(self.qlogin).click()
        self.find(self.mm).click()
        self.find(self.register_locator).click()
        time.sleep(1)
        # 账号未被注册过时点击账号注册
        if self.user_is_or_not_register(mobile) != True:
            ac = self.driver.current_activity
            self.driver.wait_activity(ac, timeout=5)
            self.find(self.username).send_keys(mobile)
            self.find(self.get_code).click()
            time.sleep(2)
            code = Dbmysql().query(
                'select content  from zxx_sms_log where mobile = {}  ORDER BY sendtime desc LIMIT 1 '.format(mobile))
            time.sleep(1)
            code_data = code[0]
            a, b = str(code_data).split('|', 1)
            self.find(self.input_code).send_keys(b)
            self.find(self.input_password_locator).send_keys(password)
            self.find(self.wancheng_locator).click()
            return True
        else:
            self.back_activity(2)
            return False

    def user_is_or_not_register(self, mobile):
        '''
        判断账号是否注册成功
        :return:
        '''
        raw_userid = Dbmysql().query(
            'select id  from zxx_sms_log where mobile = {}  ORDER BY sendtime desc LIMIT 1 '.format(mobile))
        if len(raw_userid) != 0:
            print(mobile, '已经被注册')
            return True
        elif len(raw_userid) == 0:
            print('该手机号未被注册过')
            return False

    def registered_verify(self, mobile):
        '''
        已注册账号再次注册的验证
        :param mobile:
        :return:
        '''
        self.find(self.qlogin).click()
        self.find(self.mm).click()
        self.find(self.register_locator).click()
        ac = self.driver.current_activity
        self.driver.wait_activity(ac, timeout=5)
        self.find(self.username).send_keys(mobile)
        self.find(self.get_code).click()
        if self.find('xpath', '//*[@text="验证码错误"]').text == '验证码错误':
            print('账号已注册过')
            self.back_activity(2)
            return True
        else:
            self.back_activity(2)
            return False

    def get_back_password(self, mobile, password):
        '''
        找回密码
        :param mobile:
        :param password:
        :return:
        '''
        self.find(self.qlogin).click()
        self.find(self.mm).click()
        self.find(self.forget_password).click()
        ac = self.driver.current_activity
        self.driver.wait_activity(ac, timeout=5)
        self.find(self.username).send_keys(mobile)
        self.find(self.next_step).click()
        time.sleep(1)
        code = Dbmysql().query(
            'select content  from zxx_sms_log where mobile = {}  ORDER BY sendtime desc LIMIT 1 '.format(mobile))
        code_data = code[0]
        a, b = str(code_data).split('|', 1)
        self.find(self.input_code).send_keys(b)
        self.find(self.new_password).send_keys(password)
        self.find(self.confirm_password).send_keys(password)
        self.find(self.submit).click()
        self.find(self.mm).click()
        if self.login_succeed(mobile, password) == True:
            return True
        else:
            return False

    def not_register_password(self, mobile):
        not_register_logcator = ("xpath", "//*[contains(@text,'验证码错误')]")
        self.find(self.qlogin).click()
        self.find(self.mm).click()
        self.find(self.forget_password).click()
        ac = self.driver.current_activity
        self.driver.wait_activity(ac, timeout=5)
        self.find(self.username).send_keys(mobile)
        self.find(self.next_step).click()
        if self.is_element(not_register_logcator):
            self.back_activity(2)
            print('该手机号未注册')
            return True

    def goto_index(self):
        self.find(self.index).click()
        return IndexPage(self.driver)

    def goto_pingce_res(self):

        # cat_res = ('id','view_result_tv')
        yuyue_teacher = ('text', '专家咨询')  # 首页专家资讯
        self.find_element(yuyue_teacher).click()
        return ShengyaTeacher(self.driver)

    def goto_guihua_open_course(self):
        cat_more_open_course = (
            'xpath', '//*[contains(@resource-id,"com.znb.zxx:id/contentTv") and contains(@text,"查看更多公开课")]')
        study_locator = ('id', 'com.znb.zxx:id/dialog_receive_btn')  # 弹窗立即学习
        course_title = ('id', 'com.znb.zxx:id/courseNameTv')  # 课程详情的课程标题
        courese_list_title = ('xpath',
                              '//*[@class="androidx.recyclerview.widget.RecyclerView"]/android.view.ViewGroup[1]/android.widget.TextView[1]')  # 课程列表中的课程标题
        time.sleep(2)
        self.swipeUp()
        # self.go_swipe(0.5, 0.8, 0.5, 0.5)
        self.find(cat_more_open_course).click()
        self.find(courese_list_title).click()
        self.page(self.index)

    def verify_play_normal(self):
        '''
        验证视频是否播放
        :return:
        '''
        play_button = (
        'xpath', '//*[@resource-id="com.znb.zxx:id/videoFrontCoverContainer"]/android.widget.ImageView[2]')
        if self.is_element(play_button) == False:
            print("视频播放成功")
            self.page(('xpath', '//*[@text="首页"]'))
            return True
        else:
            print("视频播放失败")
            self.page('//*[@text="首页"]')
            return False

    def play_video(self):
        course_title = ('xpath',
                        '//*[@resource-id="com.znb.zxx:id/teacherRecyclerView"]/android.view.ViewGroup[1]/android.widget.TextView[1]')
        course_detail_study = ('text', '立即学习')  # 课程详情里的立即学习
        play_video = ('id', 'com.znb.zxx:id/item_table_course_status_tv')  # 看回放/看视频
        quit_play_vedio = ('id', 'confirm_exit')
        free_get = ('xpath', '//*[@text="免费领取"]')
        bofang = ('id', 'barrage_view')
        self.find(self.index).click()
        self.find(course_title).click()
        if self.is_element(free_get) == True:
            self.find(free_get).click()
        if self.is_element(course_detail_study) == False:
            self.back_activity(1)
            return True
        self.find_element(course_detail_study).click()
        self.find(play_video).click()
        self.driver.update_settings({"waitForIdleTimeout": 0})
        self.find(bofang).click()
        time.sleep(2)

    def goto_guihua_news(self):
        cat_more_news = ('xpath', '//*[contains(@resource-id,"com.znb.zxx:id/contentTv") and contains(@text,"查看更多资讯")]')
        news_title_locator = ('xpath',
                              '//*[@resource-id="com.znb.zxx:id/jobNewsRecyclerView"]/android.view.ViewGroup[1]/android.widget.TextView[1]')
        link_course = ('xpath', '//*[@id="baseView"]/div/div[1]/div[3]/p[2]/a')
        link_url = ('xpath', '/html/body/div/div[1]/div/div/div[1]/div[3]/p[3]/a')
        list_news_title = (
            'xpath',
            '//*[@resource-id="com.znb.zxx:id/recyclerView"]/android.view.ViewGroup[1]/android.widget.TextView[1]')

        self.find_by_scroll('职业规划资讯')
        # self.go_swipe(0.6,0.8,0.5,0.3)
        self.find(news_title_locator).click()
        print(self.driver.contexts)  # 打印所有的环境
        time.sleep(3)
        self.driver.switch_to.context(self.driver.contexts[1])
        time.sleep(1)
        print(self.driver.current_context)  # 获取当前的环境，看是否切换成功
        self.find(link_course).click()
        time.sleep(1)
        self.driver.back()
        self.find(link_url).click()
        time.sleep(1)
        self.driver.switch_to.context(self.driver.contexts[0])  # 切换会native
        self.back_activity(3)
        self.find(cat_more_news).click()
        self.find(list_news_title).click()
        self.back_activity(2)
        return True

    def goto_kaoyan(self):
        kaoyan = ('xpath', '//*[@text="考研"]')
        self.find(kaoyan).click()
        return KaoYan(self.driver)

    def goto_zsb(self):
        zsb = ('text', '专升本')
        self.find_element(zsb).click()
        return Shengben(self.driver)

    def goto_kaobian(self):
        kb = ("xpath", "//*[@text='考公考编']")
        self.find_element(kb).click()
        return Kaobian(self.driver)

    def goto_cert(self):
        zgz = ("xpath", "//*[@text='资格证书']")
        self.find_element(zgz).click()
        return CertiFication(self.driver)

    def goto_course(self):
        course = ('xpath', '//*[@text="课程"]')
        self.find(course).click()
        return Course(self.driver)

    def goto_base(self):
        base = ('xpath', '//*[@text="基地"]')
        self.find(base).click()
        return Zxx_Base(self.driver)
